home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / u_man / cat1 / perlstyle.Z / perlstyle
Encoding:
Text File  |  1998-10-28  |  11.0 KB  |  397 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))    22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       perlstyle - Perl style guide
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.       Each programmer will,    of course, have    his or her own
  13.       preferences in regards to formatting,    but there are some
  14.       general guidelines that will make your programs easier to
  15.       read,    understand, and    maintain.
  16.  
  17.       The most important thing is to run your programs under the
  18.       ----wwww flag at all times.     You may turn it off explicitly    for
  19.       particular portions of code via the $^W variable if you
  20.       must.     You should also always    run under use strict or    know
  21.       the reason why not.  The use sigtrap and even    use
  22.       diagnostics pragmas may also prove useful.
  23.  
  24.       Regarding aesthetics of code lay out,    about the only thing
  25.       Larry    cares strongly about is    that the closing curly brace
  26.       of a multi-line BLOCK    should line up with the    keyword    that
  27.       started the construct.  Beyond that, he has other
  28.       preferences that aren't so strong:
  29.  
  30.       +o   4-column indent.
  31.  
  32.       +o   Opening curly on same line as keyword, if    possible,
  33.           otherwise    line up.
  34.  
  35.       +o   Space before the opening curly of    a multi-line BLOCK.
  36.  
  37.       +o   One-line BLOCK may be put    on one line, including
  38.           curlies.
  39.  
  40.       +o   No space before the semicolon.
  41.  
  42.       +o   Semicolon    omitted    in "short" one-line BLOCK.
  43.  
  44.       +o   Space around most    operators.
  45.  
  46.       +o   Space around a "complex" subscript (inside brackets).
  47.  
  48.       +o   Blank lines between chunks that do different things.
  49.  
  50.       +o   Uncuddled    elses.
  51.  
  52.       +o   No space between function    name and its opening
  53.           parenthesis.
  54.  
  55.       +o   Space after each comma.
  56.  
  57.       +o   Long lines broken    after an operator (except "and"    and
  58.           "or").
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))    22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))
  71.  
  72.  
  73.  
  74.       +o   Space after last parenthesis matching on current line.
  75.  
  76.       +o   Line up corresponding items vertically.
  77.  
  78.       +o   Omit redundant punctuation as long as clarity doesn't
  79.           suffer.
  80.  
  81.       Larry    has his    reasons    for each of these things, but he
  82.       doesn't claim    that everyone else's mind works    the same as
  83.       his does.
  84.  
  85.       Here are some    other more substantive style issues to think
  86.       about:
  87.  
  88.       +o   Just because you _C_A_N do something    a particular way
  89.           doesn't mean that    you _S_H_O_U_L_D do it that way.  Perl is
  90.           designed to give you several ways    to do anything,    so
  91.           consider picking the most    readable one.  For instance
  92.  
  93.           open(FOO,$foo) || die    "Can't open $foo: $!";
  94.  
  95.           is better    than
  96.  
  97.           die "Can't open $foo:    $!" unless open(FOO,$foo);
  98.  
  99.           because the second way hides the main point of the
  100.           statement    in a modifier.    On the other hand
  101.  
  102.           print    "Starting analysis\n" if $verbose;
  103.  
  104.           is better    than
  105.  
  106.           $verbose && print "Starting analysis\n";
  107.  
  108.           because the main point isn't whether the user typed ----vvvv
  109.           or not.
  110.  
  111.           Similarly, just because an operator lets you assume
  112.           default arguments    doesn't    mean that you have to make use
  113.           of the defaults.    The defaults are there for lazy
  114.           systems programmers writing one-shot programs.  If you
  115.           want your    program    to be readable,    consider supplying the
  116.           argument.
  117.  
  118.           Along the    same lines, just because you _C_A_N omit
  119.           parentheses in many places doesn't mean that you ought
  120.           to:
  121.  
  122.           return print reverse sort num    values %array;
  123.           return print(reverse(sort num    (values(%array))));
  124.  
  125.           When in doubt, parenthesize.  At the very    least it will
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))    22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))
  137.  
  138.  
  139.  
  140.           let some poor schmuck bounce on the % key    in vvvviiii.
  141.  
  142.           Even if you aren't in doubt, consider the    mental welfare
  143.           of the person who    has to maintain    the code after you,
  144.           and who will probably put    parentheses in the wrong
  145.           place.
  146.  
  147.       +o   Don't go through silly contortions to exit a loop    at the
  148.           top or the bottom, when Perl provides the    last operator
  149.           so you can exit in the middle.  Just "outdent" it    a
  150.           little to    make it    more visible:
  151.  
  152.           LINE:
  153.               for (;;) {
  154.               statements;
  155.             last LINE if $foo;
  156.               next LINE if /^#/;
  157.               statements;
  158.               }
  159.  
  160.  
  161.       +o   Don't be afraid to use loop labels--they're there    to
  162.           enhance readability as well as to    allow multilevel loop
  163.           breaks.  See the previous    example.
  164.  
  165.       +o   Avoid using _g_r_e_p() (or _m_a_p()) or `backticks` in a    void
  166.           context, that is,    when you just throw away their return
  167.           values.  Those functions all have    return values, so use
  168.           them.  Otherwise use a _f_o_r_e_a_c_h() loop or the _s_y_s_t_e_m()
  169.           function instead.
  170.  
  171.       +o   For portability, when using features that    may not    be
  172.           implemented on every machine, test the construct in an
  173.           eval to see if it    fails.    If you know what version or
  174.           patchlevel a particular feature was implemented, you can
  175.           test $] ($PERL_VERSION in    English) to see    if it will be
  176.           there.  The Config module    will also let you interrogate
  177.           values determined    by the CCCCoooonnnnffffiiiigggguuuurrrreeee program when Perl was
  178.           installed.
  179.  
  180.       +o   Choose mnemonic identifiers.  If you can't remember what
  181.           mnemonic means, you've got a problem.
  182.  
  183.       +o   While short identifiers like $gotit are probably ok, use
  184.           underscores to separate words.  It is generally easier
  185.           to read $var_names_like_this than    $VarNamesLikeThis,
  186.           especially for non-native    speakers of English. It's also
  187.           a    simple rule that works consistently with
  188.           VAR_NAMES_LIKE_THIS.
  189.  
  190.           Package names are    sometimes an exception to this rule.
  191.           Perl informally reserves lowercase module    names for
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))    22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))
  203.  
  204.  
  205.  
  206.           "pragma" modules like integer and    strict.     Other modules
  207.           should begin with    a capital letter and use mixed case,
  208.           but probably without underscores due to limitations in
  209.           primitive    file systems' representations of module    names
  210.           as files that must fit into a few    sparse bytes.
  211.  
  212.       +o   You may find it helpful to use letter case to indicate
  213.           the scope    or nature of a variable. For example:
  214.  
  215.           $ALL_CAPS_HERE   constants only (beware clashes with perl vars!)
  216.           $Some_Caps_Here  package-wide    global/static
  217.           $no_caps_here       function scope my() or local() variables
  218.  
  219.           Function and method names    seem to    work best as all
  220.           lowercase.  E.g.,    $obj->_a_s__s_t_r_i_n_g().
  221.  
  222.           You can use a leading underscore to indicate that    a
  223.           variable or function should not be used outside the
  224.           package that defined it.
  225.  
  226.       +o   If you have a really hairy regular expression, use the
  227.           /x modifier and put in some whitespace to    make it    look a
  228.           little less like line noise.  Don't use slash as a
  229.           delimiter    when your regexp has slashes or    backslashes.
  230.  
  231.       +o   Use the new "and"    and "or" operators to avoid having to
  232.           parenthesize list    operators so much, and to reduce the
  233.           incidence    of punctuation operators like && and ||.  Call
  234.           your subroutines as if they were functions or list
  235.           operators    to avoid excessive ampersands and parentheses.
  236.  
  237.       +o   Use here documents instead of repeated _p_r_i_n_t()
  238.           statements.
  239.  
  240.       +o   Line up corresponding things vertically, especially if
  241.           it'd be too long to fit on one line anyway.
  242.  
  243.           $IDX = $ST_MTIME;
  244.           $IDX = $ST_ATIME     if $opt_u;
  245.           $IDX = $ST_CTIME     if $opt_c;
  246.           $IDX = $ST_SIZE     if $opt_s;
  247.  
  248.           mkdir    $tmpdir, 0700 or die "can't mkdir $tmpdir: $!";
  249.           chdir($tmpdir)      or die "can't chdir $tmpdir: $!";
  250.           mkdir    'tmp',     0777 or die "can't mkdir $tmpdir/tmp: $!";
  251.  
  252.  
  253.       +o   Always check the return codes of system calls.  Good
  254.           error messages should go to STDERR, include which
  255.           program caused the problem, what the failed system call
  256.           and arguments were, and (VERY IMPORTANT) should contain
  257.           the standard system error    message    for what went wrong.
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))    22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))
  269.  
  270.  
  271.  
  272.           Here's a simple but sufficient example:
  273.  
  274.           opendir(D, $dir)     or die "can't opendir $dir: $!";
  275.  
  276.  
  277.       +o   Line up your transliterations when it makes sense:
  278.  
  279.           tr [abc]
  280.              [xyz];
  281.  
  282.  
  283.       +o   Think about reusability.    Why waste brainpower on    a
  284.           one-shot when you    might want to do something like    it
  285.           again?  Consider generalizing your code.    Consider
  286.           writing a    module or object class.     Consider making your
  287.           code run cleanly with use    strict and ----wwww in effect.
  288.           Consider giving away your    code.  Consider    changing your
  289.           whole world view.     Consider... oh, never mind.
  290.  
  291.       +o   Be consistent.
  292.  
  293.       +o   Be nice.
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))    22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.      Page 6                        (printed 10/23/98)
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.